package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.City;
import service.ICityService;
import service.impl.CityServiceImpl;
import util.LayData;

@Controller
@RequestMapping("/City")
public class CityController {

    @Autowired
    private ICityService cityService = new CityServiceImpl();

    @GetMapping("/Cities")
    @ResponseBody
    public LayData getAllCities(){
        LayData ld = new LayData();

        ld.setCode(0);
        ld.setMsg("");
        ld.setData(cityService.getAllCities());

        return ld;
    }

    @PostMapping("/CityByCid/{cid}")
    @ResponseBody
    public City getCityByCid(@PathVariable(value = "cid") int cid){
        return cityService.getCityByCid(cid);
    }

    @PostMapping("/CityUpdate")
    @ResponseBody
    public LayData updateCity(@RequestBody City c){
        LayData ld = new LayData();

        boolean b = cityService.updateCity(c);
        if (b){
            ld.setCode(0);
        }
        return ld;
    }

    @PostMapping("/CitySave")
    @ResponseBody
    public LayData saveCity(@RequestBody City c){
        LayData ld = new LayData();

        boolean b = cityService.saveCity(c);
        if (b){
            ld.setCode(0);
        }

        return ld;
    }

    @PostMapping("/CityDelete/{cid}")
    @ResponseBody
    public boolean deleteCity(@PathVariable("cid") int cid){
        return cityService.deleteCity(cid);
    }
}
